<template>
    <div class="user">
        <basic-container>
            <el-row :span="24">
                <el-col
                        :xs="24"
                        :sm="24"
                        :md="5"
                        class="user__tree">
                    <avue-tree
                        class="tree"
                        :option="treeOption"
                        :data="treeData"
                        :expandAll="true"
                        @node-click="nodeClick"/>
                </el-col>
                <el-col
                        :xs="24"
                        :sm="24"
                        :md="19"
                        class="user__main">
                    <avue-crud
                            ref="crud"
                            :option="tableOption"
                            :page.sync="page"
                            :table-loading="tableLoading"
                            @on-load="getList"
                            :data="tableData"
                            @row-del="rowDel"
                            @search-change="searchChange"
                            @refresh-change="refreshChange">
                        <template slot="menu" slot-scope="scope">
                            <el-button type="text" size="mini" icon="el-icon-edit"
                                       @click="handleUpdate(scope.row,scope.index)">编辑
                            </el-button>
                        </template>
                        <template slot="menuLeft">
                            <el-button type="primary" icon="el-icon-plus" size="small" @click="addArea">新增区域</el-button>
                        </template>
                        <template slot="menuLeft">
                            <area_import ref="areaImport" style="display: inline"/>
                        </template>
                    </avue-crud>
                    <el-dialog :title="areaType == 0 ? '编辑区域' : '新增区域'" :visible.sync="manVisible" width="35%">
                        <el-form ref="form" :model="form" label-width="120px">
                            <el-form-item label="区域名称">
                                <el-input v-model="form.areaName"></el-input>
                            </el-form-item>
                            <el-form-item label="父级区域">
                                <el-cascader 
                                    clearable
                                    class="fill-width"
                                    v-model="selectParent"
                                    style="float: left"
                                    :options="areaList"
                                    :change-on-select="true"
                                />
                            </el-form-item>
                            <el-form-item label="排序值">
                                <el-input-number v-model="form.sort" :min="1" :max="10000"></el-input-number>
                            </el-form-item>
                            <el-form-item>
                                <el-button @click="close">取 消</el-button>
                                <el-button type="primary" @click="save_form">确 定</el-button>
                            </el-form-item>
                        </el-form>
                    </el-dialog>
                </el-col>
            </el-row>
        </basic-container>
    </div>

</template>

<script>
    import {addObj, areaTree, delObj, editArea, fetchList} from '@/api/crm/area';
    import {tableOption} from '@/const/crud/crm/area';
    import area_import from "@/components/crm/excel_import/sales_area_import";
    import { formatCascader } from '@/const/common/tool';

    export default {
        name: 'SysUser',
        data() {
            return {
                treeOption: {
                    nodeKey: 'id',
                    addBtn: false,
                    menu: false,
                    defaultExpandAll:true,
                    props: {
                        label: 'areaName',
                        value: 'id'
                    }
                },
                tableData: [],
                areaList: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
                form: {},
                manVisible: false,
                treeData: [],
                areaType: 0,
                selectParent:[]
            }
        },
        components: {
            area_import
        },
        created() {
            this.init();
        },
        methods: {
            init() {
                this.getDistrictList();
            },
            async getDistrictList() {
                let res = await areaTree();
                this.treeData = formatCascader(res.data.data, 'areaName', 'areaSn');
            },
            nodeClick(data) {
                console.log(data);
                this.getList({
                    currentPage: 1,
                    pageSize: 20,
                }, {
                    parentId: data.id});
            },
            getList(page, params) {
                this.tableLoading = true
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
                areaTree().then(res => {
                    this.areaList = formatCascader(res.data.data, 'areaName', 'id');
                })
            },
            rowDel: function (row, index) {
                var _this = this
                this.$confirm('是否确认删除区域名称为' + row.areaName, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.id)
                }).then(data => {
                    _this.tableData.splice(index, 1)
                    _this.$message({
                        showClose: true,
                        message: '删除成功',
                        type: 'success'
                    })
                    this.getList(this.page)
                }).catch(function (err) {
                })
            },
            /**
             * @title 数据更新
             * @param row 为当前的数据
             * @param index 为当前更新数据的行数
             * @param done 为表单关闭函数
             *
             **/
            handleUpdate: function (row) {
                this.manVisible = true;
                this.form = row;
                this.areaType = 0;
                this.selectParent = this.form.parentId;
            },
            addArea: function () {
                this.manVisible = true;
                this.form = {
                    sort: 100
                };
                this.areaType = 1;
            },
            close() {
                this.getList(this.page);
                this.manVisible = false;
            },
            save_form() {
                let params = {
                    areaName: this.form.areaName,
                    parentId: Array.isArray(this.selectParent) ? (this.selectParent.length > 0 ? this.selectParent[this.selectParent.length - 1] : '') : this.selectParent,
                    sort: this.form.sort,
                    belongDept: this.form.belongDept,
                }
                if (this.areaType == 0) {
                    params.id = this.form.id;
                }
                editArea(params).then(response => {
                    if (this.areaType == 0) {
                        this.$message.success('编辑成功')
                    } else {
                        this.$message.success('新增成功')
                    }
                    this.manVisible = false;
                    this.getList(this.page);
                })
            },
            /**
             * @title 数据添加
             * @param row 为当前的数据
             * @param done 为表单关闭函数
             *
             **/
            handleSave: function (row, done) {
                addObj(row).then(data => {
                    this.tableData.push(Object.assign({}, row))
                    this.$message({
                        showClose: true,
                        message: '添加成功',
                        type: 'success'
                    })
                    done()
                    this.getList(this.page)
                })
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
}
        }
    }
</script>
<style lang="scss">
    .tree {
        max-height: 550px;
        overflow-x: scroll;
    }
    .user {
        height: 100%;

        &__tree {
            padding-top: 3px;
            padding-right: 20px;
        }

        &__main {
            .el-card__body {
                padding-top: 0;
            }
        }
    }
</style>

